package com.pesking.core.sqlparser.processor;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import com.pesking.core.globalid.GlobalIdManage;
import com.pesking.core.parameter.Parameter;
import com.pesking.core.sqlparser.parameter.SqlParameter;
import com.pesking.core.sqlparser.parameter.SqlParameterProcessor;
import com.pesking.core.util.DateUtils;
import com.pesking.core.util.LogUtils;
import com.pesking.core.util.StringUtils;

/**
 * 日期序列号
 * @author cypei
 * @since 2016-12-07
 */

public class DateSerialNumberSqlParameterHandler extends SqlParameterProcessor { 
	
	
	@Override
	public void handle(StringBuilder sbSQL, Parameter parameter, List<SqlParameter> processors,SqlParameter sqlParameter) {
		String name = sqlParameter.getName();
		String date = DateUtils.date2String(new Date(), "yyyyMMdd");
		String seq = "" + GlobalIdManage.generateGlobalID(name);
		seq = String.valueOf(1000000000000L + Long.parseLong(seq));
		String uuid = null;
		if (seq.length() <= 12) {
			uuid = date + seq.substring(1);
		}
		else {
			uuid = seq;
		}
		if (!StringUtils.isBlank(name)){
			parameter.setParameter(name, uuid);
		}
		LogUtils.debug( "DATE_SN参数:name[" + name + "]value[" + uuid + "]");
		sbSQL.append(uuid);
	}
	
	@Override
	public List<String> getSqlParameterType() {
		return Arrays.asList(new String []{"DATE_SN"});
	}
}
